package com.wbt.bambootown.ui.activity.launcher;

import android.support.v7.widget.AppCompatTextView;

import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.ui.activity.BaseActivity;
import com.wbt.bambootown.ui.activity.LoginActivity;
import com.wbt.bambootown.ui.activity.SetAddressActivity;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.Log;
import com.wbt.bambootown.utils.SPUtils;
import com.wbt.bambootown.utils.timer.BaseTimerTask;
import com.wbt.bambootown.utils.timer.ITimerListener;

import java.text.MessageFormat;
import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wufan on 2018/5/4.
 */

public class LauncherAtivity extends BaseActivity implements ITimerListener {
    @BindView(R.id.tv_luancher_timer)
    AppCompatTextView mTvTimer = null;

    private Timer mTimer = null;
    private int mCount = 5;

    @OnClick(R.id.tv_luancher_timer)
    void onClickTimerView() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            checkIsShowScroll();
        }
    }

    private void initTimer() {
        mTimer = new Timer();
        final BaseTimerTask task = new BaseTimerTask(this);
        mTimer.schedule(task, 0, 1000);
    }


    //判断是否显示滑动启动页
    private void checkIsShowScroll() {
        if (SPUtils.getBoolean(Cons.SPCons.IS_FIRST , true)) {
            startActivity(SetAddressActivity.class);
        }else{
            startActivity(LoginActivity.class);
        }
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    public void onTimer() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mTvTimer != null) {
                    mTvTimer.setText(MessageFormat.format("跳过\n{0}s", mCount));
                    mCount--;
                    if (mCount < 0) {
                        if (mTimer != null) {
                            mTimer.cancel();
                            mTimer = null;
                            checkIsShowScroll();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar.init();
    }

    @Override
    public void initView() {
        initTimer();
    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_luancher;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }
}
